package com.miracle.login.module.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.base.state.StateController;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.common.util.PreferencesUtils;
import com.miracle.common.util.TWather;
import com.miracle.common.util.Utils;
import com.miracle.login.R;
import com.miracle.login.R2;
import com.miracle.login.module.di.component.DaggerLoginComponent;
import com.miracle.login.module.di.module.LoginModule;
import com.miracle.login.module.mvp.contract.LoginContract;
import com.miracle.login.module.mvp.entity.request.LoginRequest;
import com.miracle.login.module.mvp.presenter.LoginPresenter;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登陆界面
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {
    LifecycleProvider<ActivityEvent> lifecycleProvider;
    @BindView(R2.id.tet_name)
    TextInputEditText mTetName;
    @BindView(R2.id.tet_pwd)
    TextInputEditText mTetPwd;
    @BindView(R2.id.tv_login)
    TextView mTvLogin;

    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
    }

    @Override
    public void initStateController() {
        super.initStateController();
    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {
        DaggerLoginComponent
                .builder()
                .appComponent(appComponent)
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onRequest(LifecycleProvider<ActivityEvent> lifecycleProvider) {
        this.lifecycleProvider = lifecycleProvider;
    }

    @OnClick({R2.id.tv_login, R2.id.tv_register})
    public void onViewClicked(View view) {
        Log.d("WJQ", "onViewClicked");

        int id = view.getId();
        if (id == R.id.tv_register) {
            showActivity(new Intent(mContext, RegisterActivity.class));
        }

        if (id == R.id.tv_login) {
            //        ARouter.getInstance()
//                .build("/main/MainActivity")
//                .navigation();

            String name = mTetName.getText().toString().trim();
            String pwd = mTetPwd.getText().toString().trim();

            String[] strs = {name, pwd};
            String[] mentions = {"昵称", "密码"};

            if (Utils.checkNullList(mTvLogin, strs, mentions)) {
                return;
            }

            LoginRequest request = new LoginRequest();
            request.setUserName(name);
            request.setPassWord(pwd);

            mPresenter.login(lifecycleProvider, request);
        }
    }

    @Override
    public void loginSucc() {
        ARouter.getInstance()
                .build("/main/MainActivity")
                .navigation();
        finish();
    }

    @Override
    public void FindANewVersion(String msg) {

    }

    @Override
    public void NoNewVersion() {

    }

    /**
     * 设置点击按钮是否可以点击
     * @param enable 是否可以点击
     */
    public void setLoginEnable(boolean enable) {
        mTvLogin.setEnabled(enable);
    }

    @Override
    public void showData(Object o) {

    }
}
